package ca.concordia.comp6231.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ServerConfiguration {
	
	private String serverName;
	private List<String> availableServerNames;
	private HashMap<String, String> hostNameList;
	private HashMap<String, Integer> portNoList;
	private HashMap<String, Integer> udpPortNoList;
	
	public ServerConfiguration(String serverName){
		loadAvailableServerNames();
		
		if(!availableServerNames.contains(serverName)){
			//throw Exception
		}
		
		this.serverName = serverName;
		setSecurityPolicy();
		loadHostNameList();
		loadPortConfiguration();
	}
	
	private static void setSecurityPolicy(){
		System.setProperty("java.security.policy","file:./security.policy");
	}
	
	private void loadAvailableServerNames(){
		availableServerNames = new ArrayList<String>();
		availableServerNames.add("SPVM");
		availableServerNames.add("SPL");
		availableServerNames.add("SPB");
	}
	
	private void loadPortConfiguration(){
		portNoList = new HashMap<String, Integer>();
		udpPortNoList = new HashMap<String, Integer>();
		int startingPort = 2020, startingUDPPort = 6780;
		for(String serverName:availableServerNames){
			portNoList.put(serverName, startingPort++);
			udpPortNoList.put(serverName, startingUDPPort++);
		}
	}
	
	private void loadHostNameList(){
		hostNameList = new HashMap<String, String>();
		for(String serverName:availableServerNames){
			hostNameList.put(serverName, "localhost");
		}
	}
	
	public Integer getPortNo(){
		return this.portNoList.get(serverName);
	}
	
	public Integer getUDPPortNo(){
		return this.udpPortNoList.get(serverName);
	}
	
	public Integer getUDPPortNo(String serverName){
		return this.udpPortNoList.get(serverName);
	}
	
	public String getHostName(){
		return hostNameList.get(this.serverName);
	}
	
	public String getHostName(String serverName){
		return hostNameList.get(serverName);
	}
	
	public List<String> getAvailableServerNames(){
		return availableServerNames;
	}
}
